package com.ruoyi.miniapp.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.miniapp.MiniAppUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.map.SoMap;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.miniapp.domain.TMiniCategory;
import com.ruoyi.miniapp.domain.TMiniHistory;
import com.ruoyi.miniapp.domain.TMiniSport;
import com.ruoyi.miniapp.domain.TMiniSportRecord;
import com.ruoyi.miniapp.service.ITMiniCategoryService;
import com.ruoyi.miniapp.service.ITMiniHistoryService;
import com.ruoyi.miniapp.service.ITMiniSportRecordService;
import com.ruoyi.miniapp.service.ITMiniSportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/miniapp/sport")
public class WxSportController extends BaseController
{
    @Autowired
    private ITMiniCategoryService itMiniCategoryService;

    @Autowired
    private ITMiniSportService itMiniSportService;

    @Autowired
    private ITMiniSportRecordService itMiniSportRecordService;

    /**
     * 查询 category 和 sport 数据并拼装
     */
    @GetMapping("/categorySportList")
    public R<List<Map<String, Object>>> categorySportList() {
        // 查询所有 category 数据
        List<TMiniCategory> categoryList = itMiniCategoryService.selectTMiniCategoryList(null);
        List<Map<String, Object>> result = new ArrayList<>();

        for (TMiniCategory category : categoryList) {
            Map<String, Object> categoryMap = new HashMap<>();
            categoryMap.put("id", category.getId());
            categoryMap.put("name", category.getCategory());
            categoryMap.put("tag", category.getTag());
            categoryMap.put("icon", category.getIcon());

            // 根据 categoryId 查询对应的 sport 数据
            TMiniSport tMiniSport=new TMiniSport();
            tMiniSport.setType(category.getId());
            List<TMiniSport> sportList = itMiniSportService.selectTMiniSportList(tMiniSport);

            List<Map<String, Object>> sportMapList = new ArrayList<>();
            for (TMiniSport sport : sportList) {
                Map<String, Object> sportMap = new HashMap<>();
                sportMap.put("id", sport.getId());
                sportMap.put("image", sport.getIcon());
                sportMap.put("name", sport.getName());
                sportMap.put("unit", sport.getTime());
                sportMap.put("calorie", sport.getCalorie());
                sportMap.put("bgColor",sport.getBgColor());
                sportMapList.add(sportMap);
            }

            categoryMap.put("list", sportMapList);
            result.add(categoryMap);
        }
        return R.ok(result, "操作成功");
    }


    /**
     * 新增运动记录
     */
    @PostMapping("/addSportRecord")
    public R addSportRecord()
    {
        SoMap so = getRequestSoMap();
        int result=itMiniSportRecordService.batchSaveSport(so);
        return result>0?R.ok():R.fail("运动记录保存失败");
    }

    /**
     * 统计运动信息
     *
     */
    @PostMapping("/dencryptSportData")
    public R dencryptSportData()
    {
        SoMap so = getRequestSoMap();
        JSONObject result=itMiniSportRecordService.dencryptSportData(so);
        return R.ok(result);
    }

    /**
     * 查询运动记录
     *
     */
    @GetMapping("/sportRecordList")
    public R sportRecordList()
    {
        SoMap so = getRequestSoMap();
        TMiniSportRecord tMiniSportRecord=new TMiniSportRecord();
        tMiniSportRecord.setUserId(SecurityUtils.getLoginUser().getMiniAppUser().getId());
        tMiniSportRecord.setOpenId(SecurityUtils.getLoginUser().getMiniAppUser().getOpenId());
        // 如果传入的date为空则设置日期为当日日期
        if (so == null || so.get("date") == null || so.get("date").toString().isEmpty()) {
            tMiniSportRecord.setRecordTime(DateUtils.getToday());
        }
        List result=itMiniSportRecordService.selectTMiniSportRecordList(tMiniSportRecord);
        return R.ok(result);
    }

    /**
     * 查询历史运动记录
     *
     */
    @GetMapping("/sportHistoryList")
    public R sportHistoryList()
    {
        SoMap so = getRequestSoMap();
        so.put("userId", SecurityUtils.getLoginUser().getMiniAppUser().getId());
        so.put("openId", SecurityUtils.getLoginUser().getMiniAppUser().getOpenId());
        PageInfo<TMiniSportRecord> list = itMiniSportRecordService.getSportHistoryList(so);
        return R.ok(list);
    }



    /**
     * 运动列表
     * @return
     */
    @GetMapping("/sportList")
    public R sportList() {
        List sportList = itMiniSportService.selectTMiniSportList(null);
        return R.ok(sportList, "操作成功");
    }

    /**
     * 统计用户运动量
     * @return
     */
    @GetMapping("/userSport")
    public R userSport() {
        SoMap so = getRequestSoMap();
        so.put("recordTime",DateUtils.getToday());
        List sportList = itMiniSportRecordService.anlysisSportType(so);
        return R.ok(sportList, "操作成功");
    }
}


